import 'package:flame/components.dart';
import 'package:flame/text.dart' show TextStyle;
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter_flame/routes/RouteGame.dart';

import '../../../bloc/game_bloc/GameBloc.dart';
import '../../../bloc/game_bloc/GameState.dart';

class TextInitSunlight extends TextComponent with HasGameReference<RouterGame> {
  static final textStyle = TextStyle(fontSize: 18, color: Colors.black);

  @override
  Future<void> onLoad() async {
    text = game.bloc.state.sunlight.toStringAsFixed(0);
    textRenderer = TextPaint(style: textStyle);
    position = Vector2(47 - width / 2, 87 - height / 2);
    // 更新文本
    add(
      FlameBlocListener<GameBloc, GameStatesState>(
        bloc: game.bloc,
        onNewState: (state) {
          text = state.sunlight.toStringAsFixed(0);
          position = Vector2(47 - width / 2, 87 - height / 2);
        },
      ),
    );
    return super.onLoad();
  }
}
